﻿using LoveEatRabbit.API.Read.Applications.Commands;
using LoveEatRabbit.API.Read.Applications.Commands.Permissions;
using LoveEatRabbit.API.Read.Applications.Commands.Roles;
using LoveEatRabbit.API.Read.Applications.Commands.Users;
using LoveEatRabbit.API.Read.DTOS;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.IdentityModel.Protocols;
using System.Runtime;
using System.Runtime.InteropServices;

namespace LoveEatRabbit.API.Read.Controllers
{
    /// <summary>
    /// API控制器—读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public ManagementController(ILogger<ManagementController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }   
        /// <summary>
        /// 测试日志方法
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public IActionResult GetCess()
        {
            try
            {
                int num1 = 1;
                int num2 = 0;
                int num3 = num1 / num2;
                return Ok(num3);
            }
            catch (Exception ex)
            {
                logger.LogError("测试方法失败进入异常"+ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 登录请求方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<APIResult<UserDTO>> LoginShow(LoginQueryCommand command)
        {
            try
            {
                logger.LogInformation("登录成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败进入异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 权限显示列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<PermissionDTO>>> PermissionShow([FromQuery] PerQueryCommand command)
        {
            try
            {
                logger.LogInformation("权限显示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限列表显示失败" + ex.Message);
                throw;  
            } 
            
        }

        /// <summary>
        /// 反填方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<PermissionDTO>>> SetPermission([FromQuery] PerIdQueryCommand command)
        {
            try
            {
                logger.LogInformation("反填成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("反填失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 用户显示方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<UserDTO>>> UserShow([FromQuery] UserQueryCommand command)
        {
            try
            {
                logger.LogInformation("用户显示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("用户显示失败"+ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 角色下拉框
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<RoleModel>>> GetRole()
        {
            try
            {
                RoleQueryCommand command = new RoleQueryCommand();
                logger.LogInformation("角色下拉框成功");

                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色下拉框失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 用户反填方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<UserModel>>> UserGet([FromQuery] UserGetQueryCommand command)
        {
            try
            {
                logger.LogInformation("用户反填成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("用户反填失败" + ex.Message);
                throw; 
            }
        }

        /// <summary>
        /// 角色显示列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<RolePerDTO>>> RoleShow([FromQuery] RolePerQueryCommand command)
        {
            try
            {
                logger.LogInformation("角色显示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色显示失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 权限复选框列表
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<PermissionModel>>> PerSelect()
        {
            try
            {
                PerSelectQueryCommand command = new PerSelectQueryCommand();
                logger.LogInformation("权限复选框列表显示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限复选框列表显示失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 角色反填方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<RolePerDTO>>> RoleBackFileGet([FromQuery] RoleBackQueryCommand command)
        {
            try
            {
                logger.LogInformation("角色反填成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色反填失败" + ex.Message);
                throw;
            }
        }
    }
}
